package com.xydtech.ecard.config.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.enums.ApiContant;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.data.DataPlatHttpUtils;
import com.xydtech.ecard.config.domain.EcardNewsChannelList;
import com.xydtech.ecard.config.mapper.EcardNewsChannelListMapper;
import com.xydtech.ecard.config.service.IEcardNewsChannelListService;
import com.xydtech.system.mapper.SysDeptMapper;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子名片新闻频道Service业务层处理
 *
 * @author JHON
 * @date 2022-04-08
 */
@Service
public class EcardNewsChannelListServiceImpl implements IEcardNewsChannelListService {
    @Autowired
    private EcardNewsChannelListMapper ecardNewsChannelListMapper;
    @Autowired
    DataPlatHttpUtils dataPlatHttpUtils;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片新闻频道
     *
     * @param channelId 电子名片新闻频道主键
     * @return 电子名片新闻频道
     */
    @Override
    public EcardNewsChannelList selectEcardNewsChannelListById(String channelId) {
        return ecardNewsChannelListMapper.selectEcardNewsChannelListById(channelId);
    }

    /**
     * 查询电子名片新闻频道列表
     *
     * @param ecardNewsChannelList 电子名片新闻频道
     * @return 电子名片新闻频道
     */
    @Override
    public List<EcardNewsChannelList> selectEcardNewsChannelListList(EcardNewsChannelList ecardNewsChannelList) {
        return ecardNewsChannelListMapper.selectEcardNewsChannelListList(ecardNewsChannelList);
    }

    /**
     * 新增电子名片新闻频道
     *
     * @param ecardNewsChannelList 电子名片新闻频道
     * @return 结果
     */
    @Override
    public int insertEcardNewsChannelList(EcardNewsChannelList ecardNewsChannelList) {

        return ecardNewsChannelListMapper.insertEcardNewsChannelList(ecardNewsChannelList);
    }

    /**
     * 接口下载电子名片新闻频道
     *
     * @param deptId 法人机构
     * @return 结果
     */
    @Override
    public AjaxResult downLoadNewsChannelList(Long deptId) {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        //接口编号
        String interfaceNumOcr = configService.selectConfigByKey(ApiContant.DATA_PLAT_NEWS_CHANNEL_KEY, dept.getDeptId());
        //参数
        Map<String, Object> paramMap = new HashMap<>();

        // 调用数据平台接口
        ResponseModel responseModel = dataPlatHttpUtils.dataPlatRequest(interfaceNumOcr, "", ApiContant.APPLETTYPE_ECARD, deptId, paramMap);

        if (responseModel.getCode() != 0) {
            return AjaxResult.error("下载电子名片新闻频道失败:" + responseModel.getMsg());
        }
        // 接口返回结果
        JSONObject responseData = JSON.parseObject(JSON.toJSONString(responseModel.getData()));
        JSONArray channelList = responseData.getJSONArray("channelList");
        for (int i = 0; i < channelList.size(); i++) {
            EcardNewsChannelList newsChannel = new EcardNewsChannelList();
            newsChannel.setChannelId(channelList.getJSONObject(i).getString("channelId"));
            newsChannel.setName(channelList.getJSONObject(i).getString("name"));
            newsChannel.setSts("0");
            newsChannel.setDeptId(deptId);
            ecardNewsChannelListMapper.insertEcardNewsChannelList(newsChannel);
        }
        return AjaxResult.success();
    }

    /**
     * 修改电子名片新闻频道
     *
     * @param ecardNewsChannelList 电子名片新闻频道
     * @return 结果
     */
    @Override
    public int updateEcardNewsChannelList(EcardNewsChannelList ecardNewsChannelList) {
        return ecardNewsChannelListMapper.updateEcardNewsChannelList(ecardNewsChannelList);
    }

    /**
     * 批量删除电子名片新闻频道
     *
     * @param ids 需要删除的电子名片新闻频道主键
     * @return 结果
     */
    @Override
    public int deleteEcardNewsChannelListByIds(String ids) {
        return ecardNewsChannelListMapper.deleteEcardNewsChannelListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片新闻频道信息
     *
     * @param id 电子名片新闻频道主键
     * @return 结果
     */
    @Override
    public int deleteEcardNewsChannelListById(Long id) {
        return ecardNewsChannelListMapper.deleteEcardNewsChannelListById(id);
    }
}
